<template>
	<div class="system-role-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="roleDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" :offset="1" class="mb20">
						<el-form-item label="学号">
							<el-input disabled v-model="state.ruleForm.studentId" placeholder="请输入学号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" :offset="1" class="mb20">
						<el-form-item label="学生姓名">
							<el-input v-model="state.ruleForm.studentName" placeholder="请输入学生姓名" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" :offset="1" class="mb20">
						<el-form-item label="班级">
							<el-select
								loading-text="加载中"
								no-match-text="无匹配数据"
								no-data-text="无数据"
								clearable
								placement="bottom-end"
								class="register-lesson"
								v-model="state.ruleForm.className"
								filterable
								:placeholder="$t('message.registerStudent.registerStudentClassPlaceholder')"
							>
								<el-option v-for="item in state.options" :key="item.value" :label="item.label" :value="item.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" :offset="1" class="mb20">
						<el-form-item label="联系电话">
							<el-input disabled v-model="state.ruleForm.phone" placeholder="请输入联系电话" clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="pupilManagementDialog">
import { ElMessage } from 'element-plus';
import { get } from 'http';
import { reactive, ref } from 'vue';
import { updatePupilInfo, lessonInfo } from '/@/api/pupil/inedx';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const roleDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		studentId: '',
		studentName: '',
		className: '',
		phone: '',
	},
	options: [{ value: '', label: '' }],
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: pupilManagementType) => {
	state.ruleForm = row;
	state.dialog.title = '修改角色';
	state.dialog.submitTxt = '修 改';
	state.dialog.isShowDialog = true;
	lessonInfo()
		.then((res) => {
			if (res.code === '2000') {
				state.options = res.data.map((item: any) => {
					return { label: item.lesson_name, value: item.lesson_id };
				});
			}
		})
		.catch((err) => {
			ElMessage.error('获取班级信息失败' + err);
		});
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = async () => {
	const updateInfo = {
		student_id: state.ruleForm.studentId,
		info: {
			student_name: state.ruleForm.studentName,
			lesson_id: state.ruleForm.className,
		},
	};
	updatePupilInfo(updateInfo)
		.then((res) => {
			if (res.code === '2000') {
				ElMessage.success('修改成功');
				closeDialog();
				emit('refresh');
			} else {
				ElMessage.error('修改失败');
			}
		})
		.catch((err) => {
			ElMessage.error('修改失败!' + err);
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
